package com.ilove.video.frame;

import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VideoControl;

import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.widget.PopupBox;
import org.kalmeo.kuix.widget.Screen;
import org.kalmeo.util.frame.Frame;
import com.ilove.core.ILOVEContants;
import com.ilove.media.model.ContentList;
import com.ilove.media.model.MediaDataProvider;
import com.ilove.model.*;
import com.ilove.music.frame.ContentListFrame;
import com.ilove.music.frame.SendGiftFrame;

public class ContentFrame implements Frame, Runnable, PlayerListener {

	// static instance of MainFrame
	public static ContentFrame instance = new ContentFrame();
	public int id;// 0 text data from vas, 1 ringtone, 2 game ,3 wallpaper
	public String contentId;
	public String contentName;
	public String avatar;
	public PopupBox pop;
	public int currPage = 1;
	public int totalPage = 1;
	public String[] vecContent = null;
	boolean isShort = false;
	private Screen screen;
	String message = "";
	String destination = "";
	private Player player;
	private VideoControl control = null;
	Thread streamThread;
	public String url;
	private final MediaDataProvider pad = new MediaDataProvider();

	/**
	 * Show the screen
	 */
	public void showScreen() {
		if (screen != null) {
			screen.setCurrent();
		}
	}
	public void onAdded() {
		try {
			prepareContent();
			// initial currPage by 1
			currPage = 1;
			// Open the Pad
			pad.setTitle(contentName);
			// Preload the screen
			screen = Kuix.loadScreen(ILOVEContants.VIDEO_SCREEN, pad);
			screen.setCurrent();
			url = "http://210.211.99.58:6868/Upload"
					+ vecContent[0].toString().trim();
			// Draw page area
			streamThread = new Thread(this);
			streamThread.start();
		} catch (Exception e) {
			Kuix.alert(e);
			e.printStackTrace();

		}
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.kalmeo.util.frame.Frame#onMessage(java.lang.Object,
	 *      java.lang.Object[])
	 */
	public boolean onMessage(Object identifier, Object[] arguments) {

		if ("askQuit".equals(identifier)) {
			Kuix.alert(Kuix.getMessage("ASK_QUIT"),
					KuixConstants.ALERT_QUESTION | KuixConstants.ALERT_YES
							| KuixConstants.ALERT_NO, "!exit", null);
			return false;
		} else if ("back".equals(identifier)) {
			System.out.println("back");
			stop();
			Kuix.getFrameHandler().removeFrame(instance);
			return false;
		} else if ("download".equals(identifier)) {
			message = "NC " + contentId;
			destination = "7585";
			Kuix
					.alert(
							"Bạn download phai tra phí (5000 đồng). Bạn có đồng ý ko?)",
							KuixConstants.ALERT_QUESTION
									| KuixConstants.ALERT_YES
									| KuixConstants.ALERT_NO, "sendsms","");
			return false;
		} else if ("gift".equals(identifier)) {
			SendGiftFrame.instance.id = id;
			SendGiftFrame.instance.contentId = contentId;
			Kuix.getFrameHandler().pushFrame(SendGiftFrame.instance);
			return false;
		} else if ("sendsms".equals(identifier)) {
			new SMSender(message, destination).send();
			return false;
		}
		return true;
	}
	public void stop() {
		if (player != null) {
			player.deallocate();
			player.close();
		}
	}
	public void prepareContent() {
		try {
			//
			ContentList content = pad.getContentData(contentId, id);
			String[] arrContent = DataAccess.split(content.name, "\n");
			String data = DataAccess.proccessData(arrContent, 320);
			vecContent = DataAccess.split(data, "@@@");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.kalmeo.util.frame.Frame#onRemoved()
	 */
	public void onRemoved() {
		// show MainFrame screen
		try {
			ContentListFrame.instance.showScreen();
			screen.cleanUp();
			screen = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void run() {
		// TODO Auto-generated method stub
		doTheStream();
	}
	public void doTheStream() {
		try {
			player = Manager.createPlayer(url);
			player.addPlayerListener(this);
			player.realize();
			control = (VideoControl) player.getControl("VideoControl");
			control.initDisplayMode(VideoControl.USE_DIRECT_VIDEO,Kuix.getCanvas());
			control.setDisplayFullScreen(true);
			control.setDisplayLocation(25, 25);
			player.start();
			control.setVisible(true);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void playerUpdate(Player arg0, String arg1, Object arg2) {
		// TODO Auto-generated method stub
	}
}
